package com.ym9527.controller;


import com.ym9527.entity.ImgList;
import com.ym9527.entity.LostAndFoundRequest;
import com.ym9527.jpa.ImageInfoJpa;
import com.ym9527.jpa.LostAndFoundJpa;
import com.ym9527.utils.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 图片上传接口
 */
@RestController
@RequestMapping("/file")
public class ImageController {



    @Resource
    ImageInfoJpa imageInfoJpa;

    @Resource
    LostAndFoundJpa lostAndFoundJpa;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
    /**
     * 这个已经默认 是 提供给流程处理 的 文件上传了
     * @param file
     * @param
     * @param request
     * @return
     */
    @RequestMapping("/fileUp")
    public R FileUp(@RequestParam(value = "file",required = false) MultipartFile file,
                    @RequestHeader(value = "LostId",required = false) Long LostId,
                    @RequestHeader(value = "ImageType",required = false) Long ImageType, HttpServletRequest request){

        MultipartFile uploadFile = file;
        //设置上传文件的位置在该项目目录下的uploadFile文件夹下，并根据上传的文件日期，进行分类保存

        String path="";
        String realPath="";
        try {
             path = ResourceUtils.getURL("classpath:").getPath()+"static/upload";
             realPath = path.replace('/', '\\').substring(1,path.length());
        }catch (Exception e){
            e.printStackTrace();

        }




        String format = sdf.format(new Date());
        File folder = new File(realPath);
        System.err.println("等待被创建的文件路径"+realPath);
        if (!folder.isDirectory()) {
            
            folder.mkdirs();
            System.err.println("当前文件已经创建完毕");
        }else{
            System.err.println("当前文件已经被创建");
        }

        String oldName = uploadFile.getOriginalFilename();
        System.err.println("oldName   "+oldName);
        String newName = UUID.randomUUID().toString() + oldName.
                substring(oldName.lastIndexOf("."), oldName.length());
        System.out.println("newName = " + newName);
        try {
            //保存文件
            File dest = new File(new File(realPath).getAbsolutePath()+ "/" + newName);

            System.err.println("dest   "+dest);

            uploadFile.transferTo(dest);

            //生成上传文件的访问路径
            String filePath ="upload" +"/"+ newName;

            /**
             * 这里是在进行文件上传之后存储在数据的操作
             */
            ImgList saveFile=new ImgList();
            saveFile.setLostId(LostId);
            saveFile.setImagePath(filePath);
            ImgList fileUp1= imageInfoJpa.save(saveFile);

            /**
             * 这表示当前上传的是首页图片
             */
            if(ImageType==1){

                LostAndFoundRequest lostAndFoundRequest=lostAndFoundJpa.getById(LostId);
                lostAndFoundRequest.setIndexImgPath(filePath);
                lostAndFoundJpa.save(lostAndFoundRequest);

            }else{
                /**
                 * 这表示当前上传的是列表图片
                 */
                ImgList imgList=new ImgList();
                imgList.setImagePath(filePath);
                imgList.setLostId(LostId);
            }

            return R.ok(fileUp1);

        } catch (IOException e) {
            e.printStackTrace();
        }



        return R.error("上传失败!");


    }




}
